package org.shoukaiseki.supersaiyan.framework.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * org.shoukaiseki.supersaiyan.framework.config.TokenConfig
 * <br>
 *
 * @author shoukaiseki ブログ http://www.shoukaiseki.top/<br> E-メール jiang28555@Gmail.com<br>
 * @date 2022-09-24 16:57:54<br>
 */
@Component
@Configuration
@ConfigurationProperties(prefix = "token")
public class TokenConfig {

    public static final Logger logger=LoggerFactory.getLogger(TokenConfig.class);

    public static final long MILLIS_SECOND = 1000;

    public static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;

    // 令牌自定义标识
    private String secret;

    // 令牌秘钥
    private String header;

    // 令牌有效期（默认30天,单位分钟）
    private int expireTime=30*24*60;

    //离过期剩余n分钟之内重新刷新令牌,该值不能超过 expireTime
    private int refreshTime=15*24*60;

    //剩余过期刷新时间
    public static long REMAINING_EXPIRATION_REFRESH_TIME=0;

    @PostConstruct
    public void init(){
        logger.debug("TokenConfig.init");
        int refreshTimeTmp=refreshTime;
        //如果剩余刷新时间大于令牌有效期,则一半时间时刷新一次令牌
        if(refreshTime>expireTime){
            refreshTimeTmp=expireTime/2;
        }
        TokenConfig.REMAINING_EXPIRATION_REFRESH_TIME=new Long(refreshTimeTmp)*MILLIS_MINUTE;
        logger.debug("expireTime={},refreshTime={},REMAINING_EXPIRATION_REFRESH_TIME={}",expireTime,refreshTime,REMAINING_EXPIRATION_REFRESH_TIME);
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public int getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(int expireTime) {
        this.expireTime = expireTime;
        logger.debug("TokenConfig.setExpireTime");
    }

    public int getRefreshTime() {
        return refreshTime;
    }

    public void setRefreshTime(int refreshTime) {
        this.refreshTime = refreshTime;
        logger.debug("TokenConfig.setRefreshTime");
    }
}
